// pages/cat/index.js
import {request} from '../../utils/request.js';
Page({
     /**
     * 页面的初始数据
     */
      data: {
        addressFull:null,
        cart:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let addressFull = wx.getStorageSync("addressFull");
        if(addressFull){
            this.setData({addressFull});
        }

        let cart = wx.getStorageSync("cart")||[];
        this.setData({cart});
    },

   
    /**  userName + provinceName +  cityName +countyName +detailInfo +telNumber
     * cityName: "哈尔滨市"
        countyName: "南岗区"
        detailInfo: "天悦国际m5-2栋1单元503"
        errMsg: "chooseAddress:ok"
        nationalCode: "230103"
        postalCode: "150006"
        provinceName: "黑龙江省"
        telNumber: "18404075932"
        userName: "张金山"
     */
    chooseAddress:function(){
        wx.chooseAddress({
            success: (result) => {
                console.log(result);
                let {userName} = result; 
                let {provinceName} = result; 
                let {cityName} = result; 
                let {countyName} = result; 
                let {detailInfo} = result; 
                let {telNumber} = result; 

                let addressFull = userName+provinceName+cityName+countyName+detailInfo +telNumber;

                wx.setStorageSync("addressFull", addressFull);
                this.setData({addressFull});




            }
        });
          
    },

    plus:function(e){
        let {index} =e.target.dataset;
        let cart = this.data.cart;
        cart [index].num = cart [index].num + 1;
        this.setData({cart});
    }

    ,
    sub:function(e){
        let {index} =e.target.dataset;
        let cart = this.data.cart;
        if(cart [index].num === 1){
           return;
        }
        cart [index].num = cart [index].num - 1 ;
        this.setData({cart})
    },


    pay:function(){
        //需要登录
        let user = wx.getStorageSync("user");
        if(!user){
            this.login();
        }else{
            this.createOrder();
            //==>生成订单 token ,获取订单号
        }
       

        
    },
    login:function(){
        //this.createOrder();
        wx.login({
          complete: (res) => {
            // console.log(res.code);
            this.getUserInfo(res.code)
          },
        })
    },
    getUserInfo:function(code){
        wx.getUserInfo({
            success: (result) => {
                // console.log(result);
                this.getToken(code ,result);
            },
           
        });
          
    },
    /***
     * code 	否 	string 	否 	执行小程序登录后获取
    encryptedData 	否 	string 	否 	执行小程序获取用户信息后得到,要保证跟code是同步刷新的
    iv 	否 	string 	否 	执行小程序获取用户信息后得到
    signature 	否 	string 	否 	执行小程序获取用户信息后得到
    rawData 	否 	string 	否 	执行小程序获取用户信息后得到
    appid 	否 	string 	否 	小程序appid(因为教学原因需要传递不同的appid)
    secret 	否 	string 	否 	小程序secret(因为教学原因需要传递不同的secret)
     */
    getToken:function(code ,user){
        console.log(user);
        request({
            url:'/user/wxlogin',
            data:{
                code,
                "appid":"wx30c6665e68b8d29d", //此处应该改成自己的 这个appid不正确
                "secret":"4e7d1b627fe89d8f9f15cdee14e6ef3e", //自出应该改成自己的，这个secret不正确
                ...user
            }
        }).then(res=>{
            console.log("登录成功之后....");
            console.log();
            wx.setStorageSync("user", res);
            wx.setStorageSync("token", res.token);
            this.createOrder();
        })
    },
    createOrder:function(){
        if(!this.data.addressFull){
            wx.showToast({
                title: '请填写地址',
                icon: 'none',
                duration: 2000,
                mask: true,
                
            });
              
            return;
        }
        let token = wx.getStorageSync("token");
        //改变订单状态 根据订单号，改变订单状态

        // const goods = this.data.cart.map(v=>{
        //     return {
        //         goods_id:v.id,
        //         goods_number:v.num,
        //         goods_price:v.price
        //     }
        // });
        // let data = {
        //     order_price:9999,
        //     consignee_addr:this.data.addressFull ,
        //     goods
        // }


        let order_price = 740.0;
        let consignee_addr = "黑龙江哈尔滨学院2号楼2615 张金山 13888888888";
        let goods = [
        {  goods_id: '1',  goods_number: 1,  goods_price: 168.0 },
        {  goods_id: '2',  goods_number: 2,  goods_price: 286.0 }
        ]
        console.log("创建订单的 数据");
        // console.log(data);
        request({
            method:'POST',
            url:'/auth/order/create',
            header:{
                "token":token
            },
            data:{ order_price, consignee_addr, goods }
        }).then(res=>{
            if(res.order_number){
                wx.showModal({
                    title: '支付',
                    content: '请输入密码进行支付',
                    showCancel: true,
                    cancelText: '取消',
                    cancelColor: '#000000',
                    confirmText: '支付',
                    confirmColor: '#3CC51F',
                    success: (result) => {
                        if (result.confirm) {
                            this.realPay(res.order_number); //输入密码 
                        }
                    },
                    fail: () => {},
                    complete: () => {}
                });
                
                  
            }
        })

       
        //跳转到会员中心
    }
    ,
    realPay:function(orderNumber){
        // wx.showToast({
        //     title: '已输入密码',
        //     icon: 'success',
        //     image: '',
        //     duration: 1500
         
        // });
        let token = wx.getStorageSync("token");

        request({
            url:'/auth/order/pay',
            header:{
                token
            },
            data:{
                
                orderNumber
            }
        }).then(success=>{
            wx.showToast({
                title: '订单支付成功',
                icon: 'none',
                duration: 1500,
                mask: false,
                success: (result) => {
                    wx.redirectTo({
                        url: '/pages/order/index'
                       
                    });
                        
                },
                fail: () => {},
                complete: () => {}
            });
              
        })
          
    }

})